<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  name: 'App',
  metaInfo() {
    return {
      title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
      titleTemplate: title => {
        return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
      }
    }
  },
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.addEventListener('unload', e => this.unloadHandler(e))
  },
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.removeEventListener('unload', e => this.unloadHandler(e))
  },
  methods: {
    beforeunloadHandler(e) {
      // debugger
      this._beforeUnload_time = new Date().getTime()
      // console.log('this._beforeUnload_time：', this._beforeUnload_time)
      e = e || window.event
      // if (e) {
      //   e.returnValue = '关闭提示'
      // }
      // // debugger
      // return '关闭提示'
    },
    unloadHandler() {
      // debugger
      // console.log('this._beforeUnload_time2：', this._beforeUnload_time)
      this._gap_time = new Date().getTime() - this._beforeUnload_time
      // console.log('this._gap_time：', this._gap_time)
      // this.$store.dispatch('LogOut');
      // 判断是窗口关闭还是刷新
      if (this._gap_time <= 5) {
        // debugger
        // 如果是登录状态，关闭窗口前，移除用户
        this.$store.dispatch('LogOut');

      } else {
        // debugger
      }
    }
  }
}
</script>
